package cli.menu;

/**
 * BudgetBuoy
 * 
 * Simplify your personal expense management
 * 
 * @team cs2103jan13-w15-3j
 */

//--------------------------------------------------------------

/**
 * MergePaymentModes Class
 * 
 * Description: 
 * 
 * 
 * @package   cli.menu
 * @author    Lai Yong Quan (A0080909Y)
 * @edited by Tan Si Ling (A0085314J)
 */
import shared.*;
import java.util.ArrayList;

import cli.tools.Process;

class MergePaymentModes extends Function{
	
	private static final String[] MERGE = {
		 "-= Main Menu > Manage Payment Modes > Merge Payment Modes =-",
		 "[b]: back [u]: undo [r]: redo [h]: home menu",
		 "",
		 "Select the source Payment Mode by keying in its index:"
	};
	
	private static final String[] SELECT = {
		 "-= Main Menu > Manage Payment Modes > Merge Payment Modes =-",
		 "[b]: back [u]: undo [r]: redo [h]: home menu",
		 "",
		"Select the destination Payment Mode by keying in its index:"
	};
	
	private static State BACK = State.MANAGEPAYMENTMODES;
	private static State CURRENT = State.MERGEPAYMENTMODES;
	
	public static State runMenu(){
		
	//	cli.print(MERGE);
		
		Mode source, destination;
		
		Object choice = tools.selectFromList(cli.getApi().getModeList(), "Payment Modes", MERGE);

		// if null object returned (back indicator)
				if (choice == null) {
					return BACK;
				}

				// if received Instruction object (divert to another menu)
				if (choice.getClass() == State.class) {
					return (State) choice;
				}

				// if choice is what was expected
				source = (Mode) choice;

				if (source.getName().equalsIgnoreCase("Loan")){
					cli.println("Unable to merge with 'Loan' as the source");
					return CURRENT;
				}

				if (source.equals(cli.getApi().getModeList().get(0))) {
					cli.println("Unable to merge with 'Uncategorised' as the source");
					return CURRENT;
				}
				
			
			//cli.print(SELECT);
				
			choice = tools.selectFromList(cli.getApi().getModeList(), "Payment Modes", SELECT);

			// if null object returned (back indicator)
			if (choice == null) {
				return State.MANAGEPAYMENTMODES;
			}

			// if received Instruction object (divert to another menu)
			if (choice.getClass() == State.class) {
				return (State) choice;
			}

			// if choice is what was expected
			destination = (Mode) choice;

			if (destination.getName().equalsIgnoreCase("Loan")){
				cli.println("Cannot move elements into loans");
				cli.println("Returning to previous menu");
				return State.MANAGEPAYMENTMODES;
			}
			
			try{
			ArrayList<Record> recordList = cli.getApi().mergeMode(source, destination);

			// Si Ling: **Keeping Track of the Actions DONE**
			tools.action.addToUndoStack((Object) source, (Object) destination,
					Process.MERGEMODES, (Object) recordList);
			// **END**
			}
			catch(Exception e){		
				cli.println("An error has occurred - aborting");
			}
				return CURRENT;
		
	}
	
}